# file      : libssl/buildfile
# copyright : Copyright (c) 2018-2019 Code Synthesis Ltd
# license   : OpenSSL and SSLeay Licenses; see accompanying LICENSE file

import int_libs = libcrypto%lib{crypto}

lib{ssl}: {h c}{**} def{libssl} file{libssl.map} $int_libs

tclass = $c.target.class

i686 = ($c.target.cpu == 'i686')

linux   = ($tclass == 'linux')
bsd     = ($tclass == 'bsd')
windows = ($tclass == 'windows')

msvc_runtime = ($c.target.system == 'win32-msvc')

gcc   = ($c.class == 'gcc')
msvc  = ($c.class == 'msvc')
mingw = ($c.target.system == 'mingw32')

# Build options.
#
# Drop -DOPENSSL_PIC, -D{L|B}_ENDIAN, -DOPENSSLDIR and -DENGINESDIR as they
# are not used in the libssl source code nor in the libcrypto public headers.
#
if! $windows
{
  # Note that the upstream package uses -pthread compiler/linker option on
  # Linux and FreeBSD. The option is currently unsupported by build2, so we
  # use -D_REENTRANT and -lpthread preprocessor/linker options instead.
  #
  # Also note that on FreeBSD and Mac OS the upstream package passes
  # -D_REENTRANT.
  #
  c.poptions += -D_REENTRANT

  if $linux
    c.poptions += -DOPENSSL_USE_NODELETE

  if $bsd
    c.poptions += -D_THREAD_SAFE
}
else
{
  c.poptions += -DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE

  # Note that upstream also passes -DOPENSSL_USE_APPLINK if compiled with VC.
  # We drop this option (see libcrypto/buildfile) for details.
  #
  if $msvc_runtime
    c.poptions += -DOPENSSL_SYS_WIN32 -D_CRT_SECURE_NO_DEPRECATE \
                  -D_WINSOCK_DEPRECATED_NO_WARNINGS

  if $msvc
    c.coptions += /Gs0 /GF /Gy
  else
    c.poptions += -D_MT
}

if ($i686 && $gcc)
  c.coptions += -fomit-frame-pointer

# Disable compiler warnings.
#
if $msvc
{
  # Disable warnings that pop up with /W3.
  #
  c.coptions += /wd4090 /wd4133 /wd4244 /wd4267
}
elif $gcc
{
  # Disable warnings that pop up with -Wall -Wextra. Upstream doesn't seem to
  # care about these and it is not easy to disable specific warnings in a way
  # that works across compilers/versions (some -Wno-* options are only
  # recognized in newer versions). There are still some warnings left that
  # appear for certain platforms/compilers. We pass them through but disable
  # treating them as errors.
  #
  c.coptions += -Wno-all -Wno-extra -Wno-error
}

c.poptions =+ "-I$src_base"

if! $windows
{
  if $linux
  {
    libs{ssl}: c.loptions += -Wl,-znodelete
    c.libs += -ldl
  }

  if ($linux || $bsd)
  {
    libs{ssl}: c.loptions += -Wl,-Bsymbolic                               \
                              "-Wl,--version-script=$src_base/libssl.map"
    c.libs += -lpthread
  }
}
else
{
  # Note that for MinGW GCC the upstream package also passes -static-libgcc.
  # We normally don't link GCC run-time statically when packaging other C
  # libraries, so let's not do it here either and see how it goes.
  #
  if $mingw
    libs{ssl}: c.loptions += -Wl,--enable-auto-image-base

  c.libs += $regex.apply(ws2_32 gdi32 crypt32,      \
                         '(.+)',                    \
                         $msvc_runtime ? '\1.lib' : '-l\1')

  if $msvc_runtime
    c.libs += advapi32.lib
}

# Export options.
#
lib{ssl}:
{
  cc.export.poptions = "-I$src_base"
  cc.export.libs = $int_libs
}

# See bootstrap.build for details.
#
if $version.pre_release
  lib{ssl}: bin.lib.version = @"-$version.project_id"
else
  lib{ssl}: bin.lib.version = @"-$abi_version"

# Install headers from the upstream openssl/ subdirectory only.
#
h{*}:         install = false
openssl/h{*}: install = include/openssl/
